package jNotesGUI;

import jNotesCore.*;
import jNotesUtilities.PropertiesGenerator;

import javax.swing.*;

import java.awt.Point;
import java.util.*;

/**
 * @author Andres Ruiz
 * 
 * Manages the organization of all notes within the selected archive.
 * 
 * This threadable class will wake up every 1 second to update all the "Notes" to mark the changes made in the GUI
 */
public class NoteContainer implements Runnable
{
	ArrayList<TextNote> Notes = new ArrayList<TextNote>();
	
	boolean exitNow = false;
	
	ArrayList<JButton> NoteLoc;
	ArrayList<JTextArea> NoteText;
	
	JFrame Main;
	
	PropertiesGenerator Properties;

	public NoteContainer(JFrame main, ArrayList<JButton> loc, ArrayList<JTextArea> text)
	{
		Main = main;
		NoteLoc = loc;
		NoteText = text;
		Notes.add(new TextNote(""));
	}
	
	public void run() 
	{
		Start();
	}
	
	public void Start()
	{
		while(!exitNow)
		{
			try
			{
				Thread.sleep(1000);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			//System.out.println("Syncng with GUI");
			
			for(int i = 0; i < NoteLoc.size(); i++)
			{
				Point Location = NoteLoc.get(i).getLocation();
				String Text = NoteText.get(i).getText();
				
				//System.out.println("Note " + (i+1) + " located at " + Location.x + " " + Location.y + " has the following text in it: " + Text);
				
				if(NoteLoc.size() == Notes.size())
				{
					Notes.get(i).setText(Text);
					Notes.get(i).moveNote(Location);
				}
				else
				{
					if(Notes.size()-1 <= i)
					{
						Notes.add(i, new TextNote(Text));
						Notes.set(i, new TextNote(Text));
						Notes.get(i).moveNote(Location);
					}
				}
				
				Properties = new PropertiesGenerator(Notes.get(i), "Text");
				//System.out.println(Properties);
			}
			
			Notes.clear();//Flush Note Cache
		}
		
	}
}
